<template>
  <div class="detail-page">
    <!-- 顶部导航 -->
    <el-row class="page-header" :gutter="10">
      <el-col :span="2" class="back-col">
        <div class="back-btn" @click="handleBack">
          <ArrowLeft /> <!-- 组件化图标（修复核心） -->
        </div>
      </el-col>
      <el-col :span="20" class="header-title">我的收藏</el-col>
      <el-col :span="2" class="manage-btn">管理</el-col>
    </el-row>

    <!-- 内容区域 -->
    <div class="content">
      <!-- 空状态 -->
      <el-empty
          v-if="count === 0"
          description="暂无收藏内容"
          class="empty-state"
      >
        <img src="https://via.placeholder.com/120x120" alt="空状态" class="empty-img" />
      </el-empty>

      <!-- 收藏列表（有数据时显示） -->
      <el-row :gutter="15" v-else>
        <el-col
            :span="8"
            :xs="12"
            v-for="(item, index) in list"
            :key="index"
            class="list-item"
        >
          <div class="item-card">
            <img :src="item.img" alt="收藏内容" class="item-img" />
            <div class="item-info">
              <p class="item-title">{{ item.title }}</p>
              <p class="item-desc">{{ item.desc }}</p>
            </div>
          </div>
        </el-col>
      </el-row>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue';
import { useRouter, useRoute } from 'vue-router';
import axios from 'axios';
// 导入Element Plus图标组件（修复核心）
import { ArrowLeft } from '@element-plus/icons-vue';

// 路由相关
const router = useRouter();
const route = useRoute();

// 接收从中央组件传递的数量
const count = ref(Number(route.query.count) || 0);

// 收藏列表数据
const list = ref([]);

// 返回上一页
const handleBack = () => {
  router.back();
};

// 获取收藏列表数据
const fetchCollectionList = async () => {
  if (count.value === 0) return;
  try {
    const res = await axios.get('http://localhost:8080/api/user/collections');
    list.value = res.data;
  } catch (err) {
    console.error('获取收藏列表失败:', err);
  }
};

onMounted(() => {
  fetchCollectionList();
});
</script>

<style scoped>
.detail-page {
  background-color: #f5f5f5;
  min-height: 100vh;
  padding: 15px;
  box-sizing: border-box;
}

.page-header {
  background-color: #fff;
  padding: 12px 0;
  border-radius: 10px;
  margin-bottom: 15px;
  align-items: center;
}

/* 返回键所在列样式 */
.back-col {
  padding-left: 15px;
}

/* 返回键样式（确保图标显示） */
.back-btn {
  font-size: 20px;
  color: #333;
  cursor: pointer;
  text-align: left;
  /* 增加点击区域 */
  width: 40px;
  height: 40px;
  display: flex;
  align-items: center;
}

.header-title {
  font-size: 18px;
  font-weight: 500;
  color: #333;
  text-align: center;
}

.manage-btn {
  font-size: 14px;
  color: #666;
  text-align: center;
  cursor: pointer;
  padding-right: 15px;
}

.content {
  padding-bottom: 20px;
}

.empty-state {
  background-color: #fff;
  padding: 40px 0;
  border-radius: 10px;
}

.empty-img {
  width: 120px;
  height: 120px;
  margin-bottom: 15px;
}

.list-item {
  margin-bottom: 15px;
}

.item-card {
  background-color: #fff;
  border-radius: 10px;
  padding: 10px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
}

.item-img {
  width: 100%;
  height: 140px;
  object-fit: cover;
  border-radius: 8px;
  margin-bottom: 8px;
}

.item-title {
  font-size: 14px;
  color: #333;
  margin: 0 0 5px 0;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.item-desc {
  font-size: 12px;
  color: #999;
  margin: 0;
}
</style>